/**
 * <p>
 * <h2>简述</h2>
 * 		<ol></ol>
 * <h2>功能描述</h2>
 * 		<ol>无</ol>
 * <h2>修改历史</h2>
 *    <ol>无</ol>
 * </p>
 * @author 作者
 * @2018-10-16 11:18:48
 * @version 1.0
 */
package com.irdstudio.bhcredit.ssm.framework.mybatis;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

/**
 * @author caijiufa
 *
 */
@Configuration
@MapperScan(basePackages= {"com.irdstudio.bhcredit.ssm.cmis.dao"})
@EnableTransactionManagement
public class AppConfig implements TransactionManagementConfigurer {

	@Autowired
	private DataSource dataSource;
	/* (non-Javadoc)
	 * @see org.springframework.transaction.annotation.TransactionManagementConfigurer#annotationDrivenTransactionManager()
	 */
	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}
	
	@Bean(name="sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactoryBean() {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		try {
			ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
			bean.setConfigLocation(resolver.getResource("classpath:mybatis/mybatisConfiguration.xml"));
			bean.setMapperLocations(resolver.getResources("classpath*:mybatis/mapper/**/*.xml"));
			bean.setDataSource(dataSource);
			return bean.getObject();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Bean
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
